package com.gin.kotlin.gf2gamecommunity.database.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.gin.kotlin.gf2gamecommunity.database.entity.GF2Account
import kotlinx.coroutines.flow.Flow
import java.time.ZonedDateTime

@Dao
interface GF2AccountDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun add(account: GF2Account)

    @Query("select * from GF2Account where current = 1")
    fun findCurrent(): GF2Account?

    @Query("select * from GF2Account")
    fun list(): List<GF2Account>


    @Query("select * from GF2Account")
    fun listFlow(): Flow<List<GF2Account>>

    @Query("delete from GF2Account where uid=:uid")
    fun delete(uid: Int): Int

    @Query("update GF2Account set current = 1 where uid=:uid")
    fun setCurrent(uid: Int): Int

    @Query("update GF2Account set token=:token,expire=:expire where uid=:uid")
    fun updateToken(uid: Int, token: String, expire: ZonedDateTime)

    @Query("update GF2Account set current = 0")
    fun clearCurrent(): Int
}